
package com.android.fitnesscenter.view.server;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.android.fitnesscenter.R;
import com.android.fitnesscenter.utils.ConfigProperties;
import com.android.fitnesscenter.utils.ConfigProperties.AuditListResult;
import com.android.fitnesscenter.utils.MapEntity;
import com.android.fitnesscenter.view.BaseActivity;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

public class ServerAuditListActivity extends BaseActivity {
    private ListView listview;
    private AuditListAdapter mAuditListAdapter;

    @ViewInject(R.id.txt_empty)
    private TextView txt_empty;

    @Override
    protected void titlebarMethod(int id) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.server_audit_list_layout);
        ViewUtils.inject(this);
        initTitleBar(getString(R.string.txt_audit));
        listview = (ListView) findViewById(R.id.list_view);
        mAuditListAdapter = new AuditListAdapter();
        listview.setAdapter(mAuditListAdapter);
        txt_empty.setVisibility(View.GONE);
        queryLockedUsers(FROM_AUDIT_LIST_ACTIVITY);
    }

    private class AuditListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            int count = (mUserList == null) ? 0 : mUserList.size();
//            if (count > 1) {
//                count = 1;
//            }
            return count;
        }

        @Override
        public Object getItem(int position) {
            return mUserList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = LayoutInflater.from(ServerAuditListActivity.this).inflate(
                        R.layout.audit_list_item_layout, null);
                viewHolder = new ViewHolder();
                viewHolder.txt_user = (TextView) convertView.findViewById(R.id.txt_user);
                viewHolder.txt_type = (TextView) convertView.findViewById(R.id.txt_type);
                viewHolder.btn_operate = (Button) convertView.findViewById(R.id.btn_operate);

                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            bindView(viewHolder, position);
            return convertView;
        }

        private void bindView(ViewHolder viewHolder, final int position) {
            MapEntity entity = mUserList.get(position);
            viewHolder.txt_user.setText(entity.getString(AuditListResult.LOGIN_NAME));
            String roleId = entity.getString(AuditListResult.ROLE_ID);
            viewHolder.txt_type.setText(ConfigProperties.getRoleStr(roleId));
            viewHolder.btn_operate.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    mCurEntity = mUserList.get(position);
                    updateUnLocked(mCurEntity);
                }
            });
        }

        public class ViewHolder {
            public TextView txt_user;
            public Button btn_operate;
            public TextView txt_type;
        }

    }

    private MapEntity mCurEntity;

    @Override
    protected void queryLockedUsersResult() {
        super.queryLockedUsersResult();
        if (mUserList == null || mUserList.size() == 0) {
            txt_empty.setVisibility(View.VISIBLE);
        } else {
            txt_empty.setVisibility(View.GONE);
        }
        mAuditListAdapter.notifyDataSetChanged();
    }

    @Override
    protected void updateUnLockedResult() {
        super.updateUnLockedResult();
        Intent intent = new Intent(ServerAuditListActivity.this, ServerPermissionActivity.class);
        Bundle bundle = new Bundle();
        bundle.putParcelable("info", mCurEntity);
        intent.putExtras(bundle);
        startActivity(intent);
    }

}
